package com.ceuer.mybatis.service;

import com.ceuer.mybatis.domain.Dept;
import com.ceuer.mybatis.mapper.DeptMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DeptServiceImpl implements DeptService{
	//@Resource
	@Autowired
	private DeptMapper deptMapper;
	
	@Override
	public PageInfo<Dept> selectForPageInfo(Dept dept) {
		PageInfo<Dept> pageInfo=PageHelper.startPage(dept.getPageNumber(), dept.getPageSize()).doSelectPageInfo(()->{
			deptMapper.selectAll();
		});
		return pageInfo;
	}
	
	@Override
	public List<Dept> selectForPage(Dept dept) {
		//只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询（Select）方法会被分页
		// PageHelper.startPage(dept.getPageNumber(), dept.getPageSize());
		// List<Dept> deptList=deptMapper.selectAll();
		
		List<Dept> deptList=PageHelper.startPage(dept.getPageNumber(), dept.getPageSize()).doSelectPage(()->{
			deptMapper.selectAll();
		});
		return deptList;
	}
	
	@Override
	public List<Dept> selectAll() {
		return deptMapper.selectAll();
	}
	
	@Override
	public Dept selectOne(Integer id) {
		return deptMapper.selectOne(id);
	}
	
	@Override
	//@Transactional
	public int add(Dept dept) {
		int num=deptMapper.add(dept);
		/*dept.setName("[西安高新技师学院]"+dept.getName());
		try {
			System.out.println(1/0);
		}catch (Exception e){
			throw new RuntimeException("除数不能为0");
		}
		deptMapper.update(dept);*/
		return num;
	}
	
	public int add1(Dept dept) {
		int num=deptMapper.add(dept);
		dept.setName("[西安高新技师学院]"+dept.getName());
		System.out.println(1/0);
		deptMapper.update(dept);
		return num;
	}
	
	@Override
	public int update(Dept dept) {
		return deptMapper.update(dept);
	}
	
	@Override
	public int delete(Integer id) {
		return deptMapper.delete(id);
	}
}
